package com.liu.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liu.reggie.common.CustomerException;
import com.liu.reggie.common.R;
import com.liu.reggie.dto.OrdersDto;
import com.liu.reggie.pojo.AddressBook;
import com.liu.reggie.pojo.Orders;
import com.liu.reggie.pojo.User;
import com.liu.reggie.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liustart
 * @create 2022-08-01 15:15
 */
@RestController
@RequestMapping("/order")
@Api(tags = "订单相关接口")
public class OrderController {
    @Autowired
    private OrdersService orderService;

    @Autowired
    private  UserService userService;

    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private OrderDetailService orderDetailService;

    @PostMapping("/submit")
    @ApiOperation("下单接口")
    public R<String> submit(@RequestBody Orders orders){
        orderService.submit(orders);
        return R.success("下单成功");

    }
    @GetMapping("/page")
    @ApiOperation("订单分页")
    public R<Page> page(int page, int pageSize, String numbers, LocalDateTime beginTime,LocalDateTime endTime){
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();

        Page<Orders> ordersPage = new Page<>(page,pageSize);
        Page<OrdersDto> dtoPage = new Page<>(page,pageSize);
        queryWrapper.like(numbers!=null,Orders::getNumber,numbers);
        if(beginTime!=null && endTime!=null){
            queryWrapper.between(Orders::getOrderTime,beginTime,endTime);
        }
        orderService.page(ordersPage, queryWrapper);
        BeanUtils.copyProperties(ordersPage,dtoPage,"records");
        List<Orders> ordersList = ordersPage.getRecords();
        List<OrdersDto> listOrdersDtos = new ArrayList<>();
        for (Orders orders : ordersList) {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(orders,ordersDto);
            Long userId = orders.getUserId();
            User user = userService.getById(userId);
            ordersDto.setUserName(user.getName());
            AddressBook addressBook = addressBookService.getById(orders.getAddressBookId());
            ordersDto.setAddress(addressBook.getDetail());
            listOrdersDtos.add(ordersDto);
        }
        dtoPage.setRecords(listOrdersDtos);
        return R.success(dtoPage);
    }

    @PutMapping
    @ApiOperation("修改配送状态")
    public R<String> update(@RequestBody Orders orders){
        if (orders==null){
            throw new CustomerException("ordes实体为空");
        }
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        Orders orders1 = orderService.getById(orders.getId());
        queryWrapper.eq(Orders::getId,orders.getId());
        orders1.setStatus(orders.getStatus());
        orderService.update(orders1,queryWrapper);
        return R.success("修改成功");
    }
}
